var ns, toast,dialog;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
        },
        list: [],
        //list: [{ "ID": 74, "Name": "打蜡", "ChargeCategoryID": 61, "UnitPrice": 10, "ischecked": false, "Cost": 0 }, { "ID": 73, "Name": "地板打蜡抛光", "ChargeCategoryID": 61, "UnitPrice": 0, "ischecked": false, "Cost": 0 }, { "ID": 72, "Name": "地板打蜡", "ChargeCategoryID": 61, "UnitPrice": 10, "ischecked": false, "Cost": 0 }, { "ID": 40, "Name": "我是测试项目", "ChargeCategoryID": 61, "UnitPrice": 2, "ischecked": false, "Cost": 0 }, { "ID": 39, "Name": "材料费", "ChargeCategoryID": 61, "UnitPrice": 0, "ischecked": false, "Cost": 0 }, { "ID": 38, "Name": "客服维修费", "ChargeCategoryID": 61, "UnitPrice": 0, "ischecked": false, "Cost": 0 }],
        sum:0
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getservicechargesummrylist',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_save: function() {
            var that = this;
            var list = [];
            for (var i = 0; i < that.list.length; i++) {
                if (that.list[i].Cost > 0) {
                    list.push(that.list[i]);
                }
            }
            if (list.length == 0) {
                api.toast({
                    msg: '请填写费用',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.post({
                action: 'dosaveservicecharge',
                ServiceID: that.form.ID,
                list: JSON.stringify(list)
            }, function(succeed, data, err) {
                if (succeed) {
                    api.sendEvent({
                        name: 'dosaveservicefeedone',
                        extra: {
                            IDList: JSON.stringify(data.IDList),
                        }
                    });
                    //刷新详情界面
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_editcost:function(item){
            var that=this;
            dialog.prompt({
                title:"",
                text:'请输入新的收款金额',
                type: 'number',
                value: item.Cost,
                buttons:['取消','确定']
            },function(ret){
                if(ret.buttonIndex == 2){
                    item.Cost = ret.text == '' ? 0 : ret.text;
                    that.updateNum();
                }
            })
        },
        updateNum:function(){
            var that=this;
            var newSum=0;
            for (var i = 0; i < that.list.length; i++) {
            if(that.list[i].ischecked){
                     newSum+=Number(that.list[i].Cost);
                }
            }
            that.sum=newSum;
        },
        choosecoat:function(item){
            var that=this;
            // that.updateNum();
            item.ischecked=!item.ischecked;
            if(item.ischecked==true){
                that.sum += Number(item.Cost);  
            };
             if(item.ischecked==false){
                that.sum = that.sum-Number(item.Cost);
             } 
        },
        open_shoufei:function(){
            var that = this;
            ns.openWin('neibubaoshi_shoufei', '收费标准', {
                id: that.form.ID
            });
        },
    },

});

apiready = function() {
    dialog = new auiDialog();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.id || 0;
    app.get_data();

    api.addEventListener({
        name: 'do_open_fee_history'
    }, function (ret, err) {
        app.open_shoufei();
    });
}
